//
//  PatientControlsController.m
//  Seizure
//
// 
// Caretaker controls for their patient

#import "PatientControlsController.h"

#import "DataManager.h"
#import "LogViewController.h"

#define SENSITIVITY_STRING @"Patient's Detection Sensitivity: %i"

@implementation PatientControlsController

@synthesize logoutButton;
@synthesize patientLogsButton;
@synthesize sensitivitySlider;
@synthesize sensitivityLabel;
@synthesize hasConfirmedPatient;

/**
 * Log the current Caretaker out.
 */ 
- (IBAction)userLoggedOut:(id)sender
{
    DataManager *dm = [DataManager sharedInstance];
    [dm userLoggedOut];
    
    [self.parentViewController.navigationController setNavigationBarHidden:NO animated:YES];
    [self.parentViewController.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)showPatientLogs:(id)sender
{
    LinkedAccount *currentAccount = [[DataManager sharedInstance] fetchPatient];
    
    //Go to the patient logs if this caretaker has a patient.
    if( currentAccount.status != STATUS_CONFIRMED )
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Display Logs" message:@"You must have a confirmed Patient before we can show your Patient's logs." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        LogViewController *patientLogsView = [[LogViewController alloc] initWithNibName:@"LogViewController" bundle:nil];
        [self.navigationController pushViewController:patientLogsView animated:YES];
    }
}

//TODO: load current patient's sensitivity and set the slider and label to that value.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Patient Controls" image:nil tag:1];
    }
    return self;
}

/**
 *  When the UISlider has been adjusted, the label must be updated.
 *  The Database must also be updated so the Patient's sensitivity
 *  algorithm will be adjusted to correspond with this value.
 */ 
- (IBAction)sensitivityHasChanged:(UISlider *)slider
{
    DataManager *dm = [DataManager sharedInstance];
    LinkedAccount *linked = [dm fetchPatient];
    
    if( linked.status == STATUS_CONFIRMED )
    {
        [slider setValue:floorf(slider.value) animated:YES];
    
        NSNumber *value = [[NSNumber alloc] initWithInt: slider.value];
        sensitivityLabel.text = [NSString stringWithFormat:SENSITIVITY_STRING,value.integerValue];
    
    
        [dm updatePatientSensitivity:linked.email  sensitivity:value];
    }
}

- (void)getPatientSensitivity
{
    if( !hasConfirmedPatient ) 
    {
        [sensitivitySlider setEnabled:NO];
        sensitivityLabel.text = @"Patient must be confirmed.";
    }
    else
    {
        [sensitivitySlider setEnabled:YES];
        LinkedAccount *l = [[DataManager sharedInstance] fetchPatient];
    
        NSNumber *num = [[DataManager sharedInstance] getPatientSensitivity:l.email];
        //NSLog(@"ViewDidLoad in PatientControlsController -- %i",num.integerValue);
    
        //Update the slider value to the current Patient value.
        sensitivitySlider.value = num.floatValue;
        //Now update the label.
        sensitivityLabel.text = [NSString stringWithFormat:SENSITIVITY_STRING,num.integerValue];
    }
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //We only want the final value they select to cause an IBAction
    sensitivitySlider.continuous=NO;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    if( [[DataManager sharedInstance] fetchPatient].status == STATUS_CONFIRMED )
    {
        hasConfirmedPatient = YES;
    }
    else
    {
        hasConfirmedPatient = NO;
    }
    
    self.navigationController.navigationBarHidden = YES;
    [self getPatientSensitivity];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
